﻿using AdminSystem.Applications;
using AdminSystem.Write.API.Command.TracebilityCommand;
using AdminSystem.Write.API.Command.TracebilitysDetile;
using AdminSystem.Write.API.DTOS;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace AdminSystem.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TracebilityController : ControllerBase
    {
        private readonly IMediator Mediator;

        public TracebilityController(IMediator mediator)
        {
            Mediator = mediator;
        }





        /// <summary>
        /// 添加溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<ApiResult<object>> AddTracebilityHandle(TracebilityCommand command)
        {

            return await Mediator.Send(command);
        }
        /// <summary>
        /// 查询溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<ApiPaging<TracebilityDTO>>> GetTracebility(int Index, int Size)
        {
            TracebilityQueryCommand command = new TracebilityQueryCommand();
            command.Index = Index;
            command.Size = Size;
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 删除溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<object>> DelHandle(TracebilityDeleteCommand command)
        {
            return await Mediator.Send(command);
        }
        /// <summary>
        /// 修改溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<object>> UpdateHandle(TracebilityUpdateCommand command)
        {
            return await Mediator.Send(command);
        }



            /// <summary>
            /// 上传图片的方法
            /// </summary>
            /// <param name="file"></param>
            /// <returns></returns>
        [HttpPost]
        public IActionResult UploadFile(IFormFile file)
        {
            var fileName = file.FileName;
            var path = Directory.GetCurrentDirectory() + @"\wwwroot\Images\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
            }
            var url = "/Images/" + fileName;
            return Ok(url);
        }

        /// <summary>
        /// 中介者溯源详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<object>> GetTracebilityDetile(int id)
        {
            TracebilitysDetileCommand command = new TracebilitysDetileCommand();
            command.Id = id;
            return await Mediator.Send(command);
        }
    }
}
